Image Enhancement

Objective: process an image so that the result is more suitable than the original image for a specific application.

Spatial Domain: image plane - direct manipulation of pixels. Methods that operate on this domain are represented by $g(x,y) = T[f(x,y)]$ and $s = T(r)$ (the second one, for simplicity), where $f(x,y)$ is the input image and $g(x,y)$ the processed image. When the neighborhood of $(x,y)$ is 1 (just the pixel itself), we say that T becomes a gray-level transformation function.

Frequency domain: based on modification of the fourier transform of an image.


In [5]:
import matplotlib.pyplot as plt
import numpy as np
from scipy import ndimage

Inverse transformation: $s = L - 1 - r$


In [6]:
%matplotlib inline
r = ndimage.imread('./assets/lena.png', flatten=True)
s = np.ones(r.shape) * 255 - r
f, (ax1, ax2, ax3) = plt.subplots(1, 3)
ax1.imshow(r, cmap="gray") 
ax1.set_title('Original')
ax2.imshow(s, cmap="gray")
ax2.set_title('Inverse')
ax3.plot(r, s)
ax3.set_title('Transformation')


Out[6]:
<matplotlib.text.Text at 0x7fa0859cdcd0>

Note that we can rewrite the function above so that we use array indexing


In [7]:
T_IDENTITY = np.arange(256)
T_NEGATIVE = 255 - T_IDENTITY
s = T_NEGATIVE[r]

plt.imshow(s, cmap="gray")


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-7-5dc145523665> in <module>()
      1 T_IDENTITY = np.arange(256)
      2 T_NEGATIVE = 255 - T_IDENTITY
----> 3 s = T_NEGATIVE[r]
      4 
      5 plt.imshow(s, cmap="gray")

IndexError: arrays used as indices must be of integer (or boolean) type

Logarithmic Transformation: $s = c * log(1 + r)$

Useful for expanding the values of dark pixels while compressing the higher level ones


In [ ]:
s = np.log(np.ones(r.shape) + r)
f, (ax1, ax2, ax3) = plt.subplots(1, 3)
ax1.imshow(r, cmap="gray") 
ax1.set_title('Original')
ax2.imshow(s, cmap="gray")
ax2.set_title('Log')
ax3.plot(r, s)
ax3.set_title('Transformation')

Contrast Stretching (Normalization)

The idea behing this transformation is to increase the dynamic range of the gray levels in the image being processed. The algorithm scans the image to find the lowest and highest pixel values currently present in the image. Call these c and d. Then each pixel P is scaled using the following function: $$P_{out} = (P_{in} - c )*(\frac{b-a}{d-c}) + a$$


In [ ]:
plt.figure(1)
plt.subplot(221)
plt.axis('off')
plt.imshow(r, cmap="gray")

plt.subplot(222)
hist, bins, patches = plt.hist(r.ravel(), 255, normed=1, facecolor='b', alpha=0.75)

plt.subplot(223)
hist, bins, patches = plt.hist(r.ravel(), 255, normed=1, histtype='step', cumulative=True)

Histogram Equalization

Threshold

The idea of thresholding is establishing a point of cisure between what is black and what is white (binary rep) given a grayscale img.


In [ ]:
plt.figure(1)
plt.subplot(231)
plt.axis('off')
plt.imshow(r, cmap="gray")

plt.subplot(232)
hist, bins, patches = plt.hist(r.ravel(), bins=60, normed=1, facecolor='b', alpha=0.75)
plt.axvline(0.5, color='r', ls='--', lw=2)

In [ ]: